//
//  GuestInfoView.m
//  QRMaster
//
//  Created by myApple on 15/4/29.
//  Copyright (c) 2015年 myApple. All rights reserved.
//

#import "GuestInfoView.h"
#import "LineView.h"
#import "RequestNetWork.h"
#import "Reachability.h"
#import "PromptInfoView.h"
#import "UIImageView+WebCache.h"
#import "TouchBtnColor.h"
#import "OpertionPlist.h"
#import "StartPageController.h"
#import "GuestLogic.h"
#import "CommunityInfoLogic.h"
#import "BackBtn.h"
#import "GroupGuestLogic.h"

#define fontSize [UIFont systemFontOfSize:14.0]
#define ViewHeigth  50

@interface GuestInfoView()<UIAlertViewDelegate>
{
    /*
     编辑状态前
     */
     UIScrollView  * InfoScrollView;
    UIImageView * TopBackGroundView;//背景图
    BOOL editFlag;//标记是编辑状态还不是编辑状态
    UILabel * userNameLabel;
    UILabel * moblieLable;
    UILabel * emailLabel;
    UILabel * sexLabel;
    UIButton * roomBtn;
    UIButton * dateTimeBtn;
    UILabel * certificateLabel;
    UIButton * callBtn;
    
    /*
     编辑中
     */
    UITextField * nameTextField;
    UIView * editingView;
    UIButton * manBtn;
    UIButton * womanBtn;
    UIImageView * manView;
    UIImageView * womanView;
    NSString * sexFlag;
    UITextField * certificateTextField;
    UIButton * editRoomBtn;
    UIButton * editDateTimeBtn;
    NSMutableDictionary * saveDict;
    NSInteger communityManager;
    UIImageView * titleImgView;
    UIImageView * editImgView;
    
    CommunityInfoLogic *timeStamp;
}

@end

@implementation GuestInfoView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        editFlag = YES;
        [self prepareHeadView];
        [self prepareEditingView];
        [self prepareScrollView];
        saveDict = [[NSMutableDictionary alloc]init];
         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(AnalysisEditData:) name:@"editAddGuest" object:nil];
    }
    
    return self;
}


-(void)setIsHaveAllocateTime:(BOOL)isHaveAllocateTime
{
    if (!isHaveAllocateTime) {
        //隐藏分配时间按钮
        dateTimeBtn.hidden = YES;
    }
    
}

-(void)prepareHeadView
{
    float ScreenWidth = [UIScreen mainScreen].bounds.size.width;
    float ScreenHeight = [UIScreen mainScreen].bounds.size.height;
    float x = 0;
    float y = 0;
    float width = ScreenWidth;
    float height = 64;
    
    UIView * topView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    topView.backgroundColor = [UIColor blackColor];
    [self addSubview:topView];
    
    y = 0;
    width = ScreenWidth;
    height = ScreenHeight / 3.0;
    TopBackGroundView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    TopBackGroundView.userInteractionEnabled = YES;
    TopBackGroundView.image = [UIImage imageNamed:@"个人背景"];
    [self addSubview:TopBackGroundView];
    
    
    UIView * alpBackView =[[UIView alloc]initWithFrame:topView.frame];
    alpBackView.backgroundColor =[UIColor clearColor];
    [self addSubview:alpBackView];
    
    x = 10;
    y = 22;
    height = 40;
    width = 40;
    UIButton * backBtn = [BackBtn initWithFrame:CGRectMake(x, y, width, height) withImage:@"24x24"];
    [backBtn addTarget:self action:@selector(BackClick) forControlEvents:UIControlEventTouchUpInside];
    [alpBackView addSubview:backBtn];
    
    //编辑
    x = ScreenWidth - 60;
    width = 40;
    y = 22;
    height = 40;
    UIView * editBackView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [alpBackView addSubview:editBackView];
    x = 10;
    width = 20;
    height = 20;
    y = 10;
    editImgView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    editImgView.image = [UIImage imageNamed:@"ic_edit"];
    editImgView.userInteractionEnabled = YES;
    [editBackView addSubview:editImgView];
    UITapGestureRecognizer * editTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(editClick)];
    [editBackView addGestureRecognizer:editTap];
}

#pragma mark -- 点击编辑按钮
-(void)editClick
{
    if (editFlag) {
        editImgView.image = [UIImage imageNamed:@"ic_save"];
        CGRect frame = InfoScrollView.frame;
        CGRect ImgFrame = TopBackGroundView.frame;
        CGRect HeadImgFrame = titleImgView.frame;
        frame.origin.y = 64;
        ImgFrame.size.height = 0;
        HeadImgFrame.origin.y = 34;
        [UIView animateWithDuration:0.3 animations:^{
            InfoScrollView.frame = frame;
            TopBackGroundView.frame = ImgFrame;
            titleImgView.frame = HeadImgFrame;
            titleImgView.alpha = 0.0;
        } completion:^(BOOL finished) {
            InfoScrollView.hidden = YES;
            editingView.hidden = NO;
            [nameTextField becomeFirstResponder];
        }];
    }
    else
    {
        [self saveClick];
    }
    
    editFlag = !editFlag;
    
}

-(void)prepareScrollView
{
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(hiddenKeyboard) name:@"KCHiddenKeyBoardNotification" object:nil];
    
    float ScreenWidth = self.frame.size.width;
    float ScreenHeight = self.frame.size.height;
    float x = 0;
    float y = TopBackGroundView.frame.size.height;
    float width = ScreenWidth;
    float height = ScreenHeight - y;
    
    InfoScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    InfoScrollView.showsVerticalScrollIndicator = YES;
    InfoScrollView.showsHorizontalScrollIndicator = NO;
    InfoScrollView.backgroundColor = RGBA(237, 238, 241, 1);
    [self addSubview:InfoScrollView];
    
    //姓名
    x = 0;
    y = 0;
    width = ScreenWidth;
    height = 50;
    UIView * nameView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    nameView.backgroundColor = RGBA(248, 248, 248, 1);
    [InfoScrollView addSubview:nameView];
    
    x = 30;
    y = 25;
    width = nameView.frame.size.width - x;
    height = 20;
    userNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    userNameLabel.font = [UIFont systemFontOfSize:14.0];
    [nameView addSubview:userNameLabel];
    
    x = 20;
    y = TopBackGroundView.frame.size.height - 30;
    width = 50;
    height = 50;
    titleImgView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    titleImgView.layer.cornerRadius = 25;
    titleImgView.layer.masksToBounds = YES;
    [self addSubview:titleImgView];
    
    //手机号
    x = 10;
    y = 5 + nameView.frame.size.height + nameView.frame.origin.y;
    width = ScreenWidth - x - 5;
    height = ViewHeigth;
    UIButton * telePhoneBtn = [[UIButton alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [telePhoneBtn addTarget:self action:@selector(callPhone:) forControlEvents:UIControlEventTouchUpInside];
    [telePhoneBtn setBackgroundColor:RGBA(248, 248, 248, 1)];
    [telePhoneBtn setBackgroundImage:[TouchBtnColor imageWithColor:RGBA(237, 238, 241, 1) size:telePhoneBtn.frame.size] forState:UIControlStateHighlighted];
    [InfoScrollView addSubview:telePhoneBtn];
    
    x = 10;
    y = 15;
    width = 60;
    height = 20;
    UILabel * telePhoneTextLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    telePhoneTextLabel.text = NSLocalizedString(@"telePhoneNum", nil);
    telePhoneTextLabel.font = fontSize;
    [telePhoneBtn addSubview:telePhoneTextLabel];
    
    x = 100;
    width = ScreenWidth - x;
    moblieLable = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    moblieLable.font = fontSize;
    [telePhoneBtn addSubview:moblieLable];
    
    x = telePhoneBtn.frame.size.width - 50;
    y = 0;
    height = 50;
    width = 50;
    callBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    callBtn.frame = CGRectMake(x, y, width, height);
    [callBtn setBackgroundColor:RGBA(125, 194, 52, 1)];
    [callBtn addTarget:self action:@selector(callPhone:) forControlEvents:UIControlEventTouchUpInside];
    [telePhoneBtn addSubview:callBtn];
    
    x = 15;
    y = 15;
    width = 20;
    height = 20;
    UIImageView * callImg = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    callImg.image = [UIImage imageNamed:@"ic_call30"];
    [callBtn addSubview:callImg];
    
    
    //邮箱号码
    x = 10;
    y = 5 + telePhoneBtn.frame.size.height + telePhoneBtn.frame.origin.y;
    width = ScreenWidth - x - 5;
    height = ViewHeigth;
    UIView * emailView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    emailView.backgroundColor = RGBA(248, 248, 248, 1);;
    [InfoScrollView addSubview:emailView];
    
    x = 10;
    y = 15;
    width = 60;
    height = 20;
    UILabel * emailTextLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    emailTextLabel.text = NSLocalizedString(@"email", nil);
    emailTextLabel.font = fontSize;
    [emailView addSubview:emailTextLabel];
    
    x = 100;
    width = ScreenWidth - x;
    emailLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    emailLabel.font = fontSize;
    [emailView addSubview:emailLabel];
    
    //性别
    x = 10;
    y = 5 + emailView.frame.size.height + emailView.frame.origin.y;
    width = ScreenWidth - x - 5;
    height = ViewHeigth;
    UIView * sexView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    sexView.backgroundColor = RGBA(248, 248, 248, 1);;
    [InfoScrollView addSubview:sexView];
    
    x = 10;
    y = 15;
    width = 60;
    height = 20;
    UILabel * sexTextLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    sexTextLabel.text = NSLocalizedString(@"sex", nil);
    sexTextLabel.font = fontSize;
    [sexView addSubview:sexTextLabel];
    
    x = 100;
    width = ScreenWidth - x;
    sexLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    sexLabel.font = fontSize;
    sexLabel.textColor = RGBA(143, 147, 148, 1);
    [sexView addSubview:sexLabel];
    
    //身份证
    x = 10;
    y = sexView.frame.size.height + sexView.frame.origin.y + 5;
    width = ScreenWidth - x - 5;
    height = ViewHeigth;
    UIView * certificateView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    certificateView.backgroundColor = RGBA(248, 248, 248, 1);;
    [InfoScrollView addSubview:certificateView];
    
    x = 10;
    y = 15;
    width = 60;
    height = 20;
    UILabel * certificateTextLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    certificateTextLabel.text = NSLocalizedString(@"certificate", nil);
    certificateTextLabel.font = fontSize;
    [certificateView addSubview:certificateTextLabel];
    
    x = 100;
    width = ScreenWidth - x;
    certificateLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    certificateLabel.font = fontSize;
    certificateLabel.textColor = RGBA(143, 147, 148, 1);
    [certificateView addSubview:certificateLabel];
    
    //开门权限
    x = 10;
    y = certificateView.frame.size.height + certificateView.frame.origin.y + 5;
    width = ScreenWidth - x - 5;
    height = ViewHeigth;
    roomBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    roomBtn.frame = CGRectMake(x, y, width, height);
    roomBtn.backgroundColor = RGBA(248, 248, 248, 1);;
    [roomBtn setBackgroundImage:[TouchBtnColor imageWithColor:RGBA(237, 238, 241, 1) size:roomBtn.frame.size] forState:UIControlStateHighlighted];
    [roomBtn addTarget:self action:@selector(roomSelect) forControlEvents:UIControlEventTouchUpInside];
    [InfoScrollView addSubview:roomBtn];
    
    x = 10;
    y = 15;
    height = 20;
    width = 120;
    UILabel * roomLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    roomLabel.text = NSLocalizedString(@"openRoomPower", nil);
    roomLabel.font = [UIFont systemFontOfSize:14.0];
    [roomBtn addSubview:roomLabel];
    
    x = roomBtn.frame.size.width - 40;
    y = 15;
    height = 20;
    width = 20;
    UIImageView * roomImgView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    roomImgView.image = [UIImage imageNamed:@"36右箭头"];
    [roomBtn addSubview:roomImgView];
    
    //时间设置
    x = 10;
    y = roomBtn.frame.size.height + roomBtn.frame.origin.y + 5;
    width = ScreenWidth - x - 5;
    height = ViewHeigth;
    dateTimeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    dateTimeBtn.frame = CGRectMake(x, y, width, height);
    dateTimeBtn.backgroundColor = RGBA(248, 248, 248, 1);;
    [dateTimeBtn setBackgroundImage:[TouchBtnColor imageWithColor:RGBA(237, 238, 241, 1) size:dateTimeBtn.frame.size] forState:UIControlStateHighlighted];
    [dateTimeBtn addTarget:self action:@selector(dateTimeSelect) forControlEvents:UIControlEventTouchUpInside];
    if (y + height > InfoScrollView.contentSize.height) {
        InfoScrollView.contentSize = CGSizeMake(ScreenWidth, y + height + 5);
    }
    [InfoScrollView addSubview:dateTimeBtn];
    
    x = 10;
    y = 15;
    height = 20;
    width = 120;
    UILabel * dateTimeLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    dateTimeLabel.text = NSLocalizedString(@"setTime", nil);
    dateTimeLabel.font = [UIFont systemFontOfSize:14.0];
    [dateTimeBtn addSubview:dateTimeLabel];
    
    x = dateTimeBtn.frame.size.width - 40;
    y = 15;
    height = 20;
    width = 20;
    UIImageView * dateTimeView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    dateTimeView.image = [UIImage imageNamed:@"36右箭头"];
    [dateTimeBtn addSubview:dateTimeView];
    
    
}

-(void)callPhone:(UIButton *)btn
{
    NSString * callString = nil;
    if (![_model.areaCode isEqualToString:[QRSingleton sharedQRSingleton].currentAreaCodeString]) {
        callString = [NSString stringWithFormat:@"+%@-%@",_model.areaCode,_model.telePhone];
    }
    else
    {
        callString = moblieLable.text;
    }

    NSString *telUrl = [NSString stringWithFormat:@"telprompt://%@",callString];
    NSURL *url = [NSURL URLWithString:telUrl];
    [[UIApplication sharedApplication] openURL:url];
}

-(void)prepareEditingView
{
    float ScreenWidth = [UIScreen mainScreen].bounds.size.width;
    float ScreenHeight = [UIScreen mainScreen].bounds.size.height;
    float x = 0;
    float y = 64;
    float width = ScreenWidth;
    float height = ScreenHeight - y;
    
    editingView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    editingView.backgroundColor = RGBA(237, 238, 241, 1);;
    editingView.hidden = YES;
    [self addSubview:editingView];
    
    //姓名
    x = 10;
    y = 5;
    width = ScreenWidth - x - 5;
    height = ViewHeigth;
    UIView * nameView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    nameView.backgroundColor = RGBA(248, 248, 248, 1);;
    [editingView addSubview:nameView];
    
    x = 10;
    y = 15;
    width = 40;
    height = 20;
    UILabel * editUserNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    editUserNameLabel.text = NSLocalizedString(@"userName", nil);
    editUserNameLabel.font = fontSize;
    [nameView addSubview:editUserNameLabel];
    
    x = 100;
    y = 10;
    width = nameView.frame.size.width - x ;
    height = 30;
    nameTextField = [[UITextField alloc]initWithFrame:CGRectMake(x, y, width, height)];
    nameTextField.font = [UIFont systemFontOfSize:14.0];
    nameTextField.borderStyle = UITextBorderStyleNone;
    [nameView addSubview:nameTextField];
    
    //身份证
    x = 10;
    y = 5 + nameView.frame.size.height + nameView.frame.origin.y;
    width = ScreenWidth - x - 5;
    height = ViewHeigth;
    UIView * certificateView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    certificateView.backgroundColor = RGBA(248, 248, 248, 1);;
    [editingView addSubview:certificateView];
    
    x = 10;
    y = 15;
    width = 100;
    height = 20;
    UILabel * editCertificateLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    editCertificateLabel.text = NSLocalizedString(@"certificateNum", nil);
    editCertificateLabel.font = fontSize;
    [certificateView addSubview:editCertificateLabel];
    
    x = 100;
    y = 10;
    width = certificateView.frame.size.width - x ;
    height = 30;
    certificateTextField = [[UITextField alloc]initWithFrame:CGRectMake(x, y, width, height)];
    certificateTextField.font = [UIFont systemFontOfSize:14.0];
    certificateTextField.borderStyle = UITextBorderStyleNone;
    [certificateView addSubview:certificateTextField];
    
    //性别
    x = 10;
    y = 5 + certificateView.frame.size.height + certificateView.frame.origin.y;
    width = ScreenWidth - x - 5;
    height = ViewHeigth;
    UIView * sexView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    sexView.backgroundColor = RGBA(248, 248, 248, 1);;
    [editingView addSubview:sexView];
    
    
    x = 10;
    y = 15;
    width = 60;
    height = 20;
    UILabel * editSexLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    editSexLabel.text = NSLocalizedString(@"sex", nil);
    editSexLabel.font = fontSize;
    [sexView addSubview:editSexLabel];
    
    x = 100;
    y = 0;
    width = 40;
    height = 50;
    manView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [sexView addSubview:manView];
    manView.userInteractionEnabled = YES;
    UITapGestureRecognizer * manTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(manClick)];
    [manView addGestureRecognizer:manTap];
    
    x = 100;
    y = 15;
    width = 20;
    height = 20;
    manBtn = [[UIButton alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [manBtn setBackgroundImage:[UIImage imageNamed:@"36_1"] forState:0];
    [manBtn setBackgroundImage:[UIImage imageNamed:@"36_2"] forState:UIControlStateSelected];
    [manBtn addTarget:self action:@selector(manClick) forControlEvents:UIControlEventTouchUpInside];
    [sexView addSubview:manBtn];
    
    x = 140;
    height = 20;
    UILabel * manLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, 40, height)];
    manLabel.text = NSLocalizedString(@"man", nil);
    manLabel.font = fontSize;
    [sexView addSubview:manLabel];
    
    x = 180;
    y = 0;
    width = 40;
    height = ViewHeigth;
    womanView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [sexView addSubview:womanView];
    womanView.userInteractionEnabled = YES;
    UITapGestureRecognizer * womanTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(womanClick)];
    [womanView addGestureRecognizer:womanTap];
    
    x += 10;
    y += 15;
    width = 20;
    height = 20;
    womanBtn = [[UIButton alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [womanBtn setBackgroundImage:[UIImage imageNamed:@"36_1"] forState:0];
    [womanBtn setBackgroundImage:[UIImage imageNamed:@"36_2"] forState:UIControlStateSelected];
    [womanBtn addTarget:self action:@selector(womanClick) forControlEvents:UIControlEventTouchUpInside];
    [sexView addSubview:womanBtn];
    
    x = 220;
    height = 20;
    UILabel * womanLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, 60, height)];
    womanLabel.text = NSLocalizedString(@"woman", nil);
    womanLabel.font = fontSize;
    [sexView addSubview:womanLabel];
    
    //开门权限
    x = 10;
    y = sexView.frame.size.height + sexView.frame.origin.y + 5;
    width = ScreenWidth - x - 5;
    height = ViewHeigth;
    editRoomBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    editRoomBtn.frame = CGRectMake(x, y, width, height);
    editRoomBtn.backgroundColor = RGBA(248, 248, 248, 1);;
    [editRoomBtn setBackgroundImage:[TouchBtnColor imageWithColor:RGBA(237, 238, 241, 1) size:editRoomBtn.frame.size] forState:UIControlStateHighlighted];
    [editRoomBtn addTarget:self action:@selector(roomSelect) forControlEvents:UIControlEventTouchUpInside];
    [editingView addSubview:editRoomBtn];
    
    x = 10;
    y = 15;
    height = 20;
    width = 120;
    UILabel * roomLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    roomLabel.text = NSLocalizedString(@"openRoomPower", nil);
    roomLabel.font = [UIFont systemFontOfSize:14.0];
    [editRoomBtn addSubview:roomLabel];
    
    x = editRoomBtn.frame.size.width - 40;
    y = 15;
    height = 20;
    width = 20;
    UIImageView * roomImgView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    roomImgView.image = [UIImage imageNamed:@"36右箭头"];
    [editRoomBtn addSubview:roomImgView];
    
    //时间设置
    x = 10;
    y = editRoomBtn.frame.size.height + editRoomBtn.frame.origin.y + 5;
    width = ScreenWidth - x - 5;
    height = ViewHeigth;
    editDateTimeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    editDateTimeBtn.frame = CGRectMake(x, y, width, height);
    editDateTimeBtn.backgroundColor = RGBA(248, 248, 248, 1);;
    [editDateTimeBtn setBackgroundImage:[TouchBtnColor imageWithColor:RGBA(237, 238, 241, 1) size:editDateTimeBtn.frame.size] forState:UIControlStateHighlighted];
    [editDateTimeBtn addTarget:self action:@selector(dateTimeSelect) forControlEvents:UIControlEventTouchUpInside];
    [editingView addSubview:editDateTimeBtn];
    
    x = 10;
    y = 15;
    height = 20;
    width = 120;
    UILabel * dateTimeLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    dateTimeLabel.text = NSLocalizedString(@"setTime", nil);
    dateTimeLabel.font = [UIFont systemFontOfSize:14.0];
    [editDateTimeBtn addSubview:dateTimeLabel];
    
    x = editDateTimeBtn.frame.size.width - 40;
    y = 15;
    height = 20;
    width = 20;
    UIImageView * dateTimeView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    dateTimeView.image = [UIImage imageNamed:@"36右箭头"];
    [editDateTimeBtn addSubview:dateTimeView];
}

-(void)resignClick
{
    [nameTextField resignFirstResponder];
    [certificateTextField resignFirstResponder];
}

-(void)saveClick
{
    [self resignClick];
    if(nameTextField.text.length > 20 || nameTextField.text.length <1)
    {
        [PromptInfoView showMessage:NSLocalizedString(@"userNameLength", nil)];
        return;
    }
    NSUserDefaults * userInfo = [NSUserDefaults standardUserDefaults];
    [saveDict setValue:[userInfo objectForKey:@"uid"] forKey:@"id"];
    [saveDict setValue:[userInfo objectForKey:@"token"] forKey:@"token"];
    [saveDict setValue:_model.guestid forKey:@"memberId"];
    [saveDict setValue:nameTextField.text forKey:@"name"];
    [saveDict setValue:sexFlag forKey:@"gender"];
    [saveDict setValue:certificateTextField.text forKey:@"identityId"];
    [saveDict setValue:_communityID forKey:@"communityId"];
    
    if([self getTimestamp:_communityID timeKey:@"customer_timestamp"] != nil)
    {
        [saveDict setValue:[self getTimestamp:_communityID timeKey:@"customer_timestamp"] forKey:@"timestamp"];
    }
    else
    {
        [saveDict setValue:@"" forKey:@"timestamp"];
    }
    RequestNetWork * rnw = [[RequestNetWork alloc]init];
    [rnw initWithPath:POST(EDITGUEST) params:saveDict block:^(id downLoadData, id error)
     {
         if([downLoadData objectForKey:@"cache"] != nil)
         {
              [PromptInfoView showMessage:NSLocalizedString(@"editSuccess", nil)];
             if([@"0" isEqualToString:[NSString stringWithFormat:@"%@",[[downLoadData objectForKey:@"cache"] objectForKey:@"status"]]]==YES)
             {
                 [self updateTimestamp:_communityID timeKey:@"customer_timestamp" timeKeyValue:[[downLoadData objectForKey:@"cache"] objectForKey:@"timestamp"]];
             }
                 NSMutableDictionary *data = [downLoadData objectForKey:@"data"];
                 if(data != nil)
                 {
                     NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
                     [dic setObject:nameTextField.text forKey:@"name"];
                     [dic setObject:sexFlag forKey:@"gender"];
                     [dic setObject:certificateTextField.text  forKey:@"identityId"];
                     [dic setObject:[self firstChar:nameTextField.text] forKey:@"firstChar"];
                     //                     [dic setObject:_model.guestid forKey:@"guestId"];
                     
                     _model.guestName =nameTextField.text;
                     _model.sex = sexFlag;
                     _model.certificaties = certificateTextField.text ;
                     GuestLogic *guestLogic = [[GuestLogic alloc] init];
                     
                     if(_groupId)
                     {
                         GroupGuestLogic *groupGuest = [[GroupGuestLogic alloc]init];
                         [groupGuest updateGroupGuestInfo:_model groupId:_groupId groupName:nil groupStartTime:nil groupEndTime:nil];
                     }
                     else
                     {
                         [guestLogic updateGuestInfo:_model.guestid communityId:_communityID dic:dic];
                     }
                     userNameLabel.text = nameTextField.text;
                     sexLabel.text = [sexFlag isEqualToString:@"1"]?NSLocalizedString(@"man", nil):NSLocalizedString(@"woman", nil);
                     editImgView.image = [UIImage imageNamed:@"ic_edit"];
                     certificateLabel.text = certificateTextField.text;
                     InfoScrollView.hidden = NO;
                     editingView.hidden = YES;
                     CGRect frame = InfoScrollView.frame;
                     CGRect ImgFrame = TopBackGroundView.frame;
                     CGRect HeadImgFrame = titleImgView.frame;
                     frame.origin.y = [UIScreen mainScreen].bounds.size.height / 3.0;
                     ImgFrame.size.height = [UIScreen mainScreen].bounds.size.height / 3.0;
                     HeadImgFrame.origin.y = [UIScreen mainScreen].bounds.size.height / 3.0 - 30;
                     [UIView animateWithDuration:0.3 animations:^{
                         InfoScrollView.frame = frame;
                         TopBackGroundView.frame = ImgFrame;
                         titleImgView.frame = HeadImgFrame;
                         titleImgView.alpha = 1.0;
                     } completion:^(BOOL finished) {
                         InfoScrollView.scrollEnabled = YES;
                     }];
                     [nameTextField resignFirstResponder];
                 }
         }
         else
         {
             [PromptInfoView showMessage:[error objectForKey:@"info"]];
         }
     }
     ];
}

-(void)womanClick
{
    if (womanBtn.selected) {
        manBtn.selected = YES;
        womanBtn.selected = NO;
        sexFlag = @"1";
       
        
    }
    else
    {
        manBtn.selected = NO;
        womanBtn.selected = YES;
        sexFlag = @"0";
        
    }
}

-(void)manClick
{
    if (manBtn.selected) {
        manBtn.selected = NO;
        womanBtn.selected = YES;
        sexFlag = @"0";
        
    }
    else
    {
        manBtn.selected = YES;
        womanBtn.selected = NO;
        sexFlag = @"1";

    }
}


-(void)roomSelect
{
    RoomSeclectViewController * rsvc = [[RoomSeclectViewController alloc]init];
    rsvc.email = _model.email;
    rsvc.mobile = _model.telePhone;
    if (_delegate && [_delegate respondsToSelector:@selector(guestInFoViewSendRoomSelect:)]) {
        [_delegate guestInFoViewSendRoomSelect:rsvc];
    }
}

-(void)dateTimeSelect
{
    if (_delegate && [_delegate respondsToSelector:@selector(guestInFoViewSendSetDateTime)]) {
        [_delegate guestInFoViewSendSetDateTime];
    }
}

-(void)setModel:(GuestModel *)model
{
    _model = model;
    nameTextField.text = model.guestName;
    userNameLabel.text = model.guestName;
    emailLabel.text = model.email;
    if (model.email == nil || [model.email isEqualToString:@""]) {
        if (![model.areaCode isEqualToString:[QRSingleton sharedQRSingleton].currentAreaCodeString]) {
            moblieLable.text = [NSString stringWithFormat:@"+%@ %@",model.areaCode,model.telePhone];
        }
        else
        {
            moblieLable.text = model.telePhone;
        }
    }
    certificateLabel.text = model.certificaties;
    certificateTextField.text = model.certificaties;
    [titleImgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",YSBHEADERIMGURL,model.headUrl]] placeholderImage:[UIImage imageNamed:@"96人像"]];
    if ([model.sex isEqualToString:@"1"]) {
        manBtn.selected = YES;
        womanBtn.selected = NO;
        sexFlag = @"1";
    }
    else
    {
        womanBtn.selected = YES;
        manBtn.selected = NO;
        sexFlag = @"0";
    }
    
   sexLabel.text = ([sexFlag isEqualToString:@"0"]?NSLocalizedString(@"woman", nil):NSLocalizedString(@"man", nil));
    if ([moblieLable.text isEqualToString:@""]|| moblieLable.text == nil) {
        callBtn.hidden = YES;
    }

}

-(void)setCommunityID:(NSString *)communityID
{
    _communityID = communityID;
    NSDictionary * communityInfo = [OpertionPlist readDataFromPlist];
    NSArray * communityArray = [communityInfo objectForKey:COMMUNITYINFOFORKEY];
    for (NSDictionary * dict in communityArray) {
        if ([[dict objectForKey:@"communityid"] isEqualToString:_communityID]) {
             communityManager = [[dict objectForKey:@"groupMId"]integerValue];
        }
    }
}

//-(void)setAllocateRoomPower:(BOOL)allocateRoomPower
//{
//    if (!allocateRoomPower && communityManager == 0) {
//        roomBtn.hidden = YES;
//        dateTimeBtn.hidden = YES;
//        editDateTimeBtn.hidden = YES;
//        editRoomBtn.hidden = YES;
//    }
//}

//编辑信息保存
-(void)AnalysisEditData:(NSNotification *)not
{
    [self BackClick];
}

-(void)BackClick
{
    if (_delegate && [_delegate respondsToSelector:@selector(guestInfoBack)]) {
//        [[NSNotificationCenter defaultCenter]postNotificationName:@"refreshGuest" object:nil];

        [_delegate guestInfoBack];
    }
}

//判断是否有网络
-(BOOL)IsHaveNetwork
{
    Reachability * reach = [Reachability reachabilityForInternetConnection];
    if([reach currentReachabilityStatus] == NotReachable) {
        [PromptInfoView showMessage:NSLocalizedString(@"noNetWork", nil)];
        return YES;
    }
    return NO;
}

-(void)hiddenKeyboard
{
    [nameTextField resignFirstResponder];
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"editAddGuest" object:nil];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"KCHiddenKeyBoardNotification" object:nil];
}

#pragma mark ----------更新时间戳和获取时间戳
-(NSString *)getTimestamp:(NSString *)communityId timeKey:(NSString *)timeKey
{
    timeStamp = [[CommunityInfoLogic alloc]init];
    NSString * str = [timeStamp getTimestamp:communityId timeKey:timeKey];
    return str;
}

-(void)updateTimestamp:(NSString *)communityId  timeKey:(NSString *)timeKey timeKeyValue:(NSString *)timeKeyValue
{
    timeStamp = [[CommunityInfoLogic alloc]init];
    [timeStamp updateTimestamp:communityId timeKey:timeKey timeKeyValue:timeKeyValue];
}


@end
